﻿using System.Collections.Generic;
using System.Linq;
using QASPRibbon.Commands;
using QASPRibbon.Controls;

namespace QASPRibbon.Helpers
{
    internal class ScriptHelper
    {
        internal static string GetCommandsScript(IEnumerable<RibbonCommand> commands, string ns)
        {
            if (commands == null || !commands.Any())
                return string.Empty;

            return
                string.Format(Resources.ScriptPageComponentInit,
                    ns,
                    string.Join(",", commands.Where(c => !c.IsStateCheck).Select(c => string.Format("'{0}'", c.FullId)).ToArray()),
                    string.Join("\n", commands.Select(c => c.ToCheckStateScript()).ToArray()),
                    string.Join("\n", commands.Select(c => c.ToActionScript()).ToArray()))
                    .Replace("$NAMESPACE$", ns);
        }

        internal static string GetPageComponentScript(string nameSpace)
        {
            return string.Format(Resources.ScriptPageComponent, nameSpace);
        }

        internal static string GetTabClientAPI(RibbonCommandRepository commandRepository, IEnumerable<RibbonQAT> qatCollection, IEnumerable<RibbonTab> tabCollection, string nameSpace)
        {
            var idStr = string.Format("'{0}'", string.Join("','", commandRepository.GetCommands().Select(x => x.ControlId).ToArray()));
            var qat = qatCollection
                .SelectMany(x => x.Controls.Select(c => "'" + c.ClientID + "'"))
                .Distinct()
                .ToArray();
            var tab = tabCollection
                .Select(x => "'" + x.ClientId + "'")
                .Distinct()
                .ToArray();
            return string.Format(Resources.ScriptClientAPI,
                //nameSpace,
                string.Join(",", tab),
                string.Join(",", qat),
                idStr);
        }
    }
}
